package com.yiren.manager.ctrl.desk;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrConsultCheck;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.desk.YrConsultCheckForm;
import com.yiren.manager.service.iface.desk.YrConsultCheckService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("consultCheck")
@Controller
public class YrConsultCheckController extends GenericController<YrConsultCheckForm, YrConsultCheck>{

	@Autowired
	private YrConsultCheckService selfService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrConsultCheckForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrConsultCheckForm> result = new HTTPResult<YrConsultCheckForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrConsultCheckForm form = tool.mapper("consultCheck", YrConsultCheckForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getConsultCheckId()||form.getConsultCheckId().isEmpty()){
			form.setConsultCheckId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrConsultCheck entity = ObjectUtils.objectToObject(form, YrConsultCheck.class);
		selfService.save(entity);
		
		return result;
	}
	
	
	/**获取所有记录*/
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrConsultCheckForm> findAll() throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrConsultCheckForm> result = new HTTPResult<YrConsultCheckForm>();
		List<YrConsultCheck> entityList = selfService.findAll();
		List<YrConsultCheckForm> formList = new ArrayList<YrConsultCheckForm>();
		for(YrConsultCheck bed : entityList){
			YrConsultCheckForm form = ObjectUtils.objectToObject(bed, YrConsultCheckForm.class);
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrConsultCheckForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrConsultCheckForm> result = new HTTPResult<YrConsultCheckForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(String id : ids){
			selfService.delete(id);
		}
		return result;
	}
	
	@RequestMapping("updateBetch")
	@ResponseBody
	public HTTPResult<YrConsultCheckForm> updateBetch(String data) throws SwallowException, MyException{
		HTTPResult<YrConsultCheckForm> result = new HTTPResult<YrConsultCheckForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		List<YrConsultCheck> form = tool.mapper("consultCheckList", new TypeReference<ArrayList<YrConsultCheck>>() {});
		
		this.selfService.updateBetch(form);
		return result;
	}
	
	@Override
	public YrConsultCheckForm toForm(YrConsultCheck e) throws Exception {
		YrConsultCheckForm form = ObjectUtils.objectToObject(e, YrConsultCheckForm.class);
		return form;
	}

	@Override
	public YrConsultCheck toEntity(YrConsultCheckForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
